package com.ruoyi.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.StudentManage;
import com.ruoyi.service.IStudentManageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生管理Controller
 * 
 * @author 孔德民
 * @date 2022-04-22
 */
@RestController
@RequestMapping("/schoolManage/student")
public class StudentManageController extends BaseController
{
    @Autowired
    private IStudentManageService studentManageService;

    /**
     * 查询学生管理列表
     */
    @PreAuthorize("@ss.hasPermi('schoolManage:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentManage studentManage)
    {
        startPage();
        List<StudentManage> list = studentManageService.selectStudentManageList(studentManage);
        return getDataTable(list);
    }

    /**
     * 导出学生管理列表
     */
    @PreAuthorize("@ss.hasPermi('schoolManage:student:export')")
    @Log(title = "学生管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody String ids)
    {
        //ids为前端传回的json字符串，首先将齐用&分割
        String[] idf = ids.split("&");
        //初始化string数组
        int x = 0;
        String[] idz = new String[idf.length];
        //将json中包含数组中的字符放到数组中
        for(String i : idf){
            String[] y = i.split("=");
            idz[x++] = y[1];
        }

        List<StudentManage> list = studentManageService.selectStudentManageListByIds(idz);
        ExcelUtil<StudentManage> util = new ExcelUtil<StudentManage>(StudentManage.class);
        //将数据链表转化成excel
        util.exportExcel(response, list, "学生管理数据");
    }

    /**
     * 获取学生管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('schoolManage:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(studentManageService.selectStudentManageById(id));
    }

    /**
     * 新增学生管理
     */
    @PreAuthorize("@ss.hasPermi('schoolManage:student:add')")
    @Log(title = "学生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentManage studentManage)
    {
        studentManage.setCreateBy(getUsername());
        return toAjax(studentManageService.insertStudentManage(studentManage));
    }

    /**
     * 修改学生管理
     */
    @PreAuthorize("@ss.hasPermi('schoolManage:student:edit')")
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentManage studentManage)
    {
        return toAjax(studentManageService.updateStudentManage(studentManage));
    }

    /**
     * 删除学生管理
     */
    @PreAuthorize("@ss.hasPermi('schoolManage:student:remove')")
    @Log(title = "学生管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentManageService.deleteStudentManageByIds(ids));
    }

    /**
    *孔德民
    *导入学生管理列表
    */
    @PreAuthorize("@ss.hasPermi('schoolManage:student:add')")
    @Log(title = "学生管理",businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception{
        ExcelUtil<StudentManage> util= new ExcelUtil<StudentManage>(StudentManage.class);
        List<StudentManage> list = util.importExcel(file.getInputStream());

        String name = getUsername();
        studentManageService.importData(list,name);

        return AjaxResult.success("厉害了");
    }
}
